﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.BaoCao
{
    public partial class NoGUI : UserControl, GUI.UctrlGUI
    {
        BLL.LapBaoCaoCongNoThangBLL _BaoCaoNoBLL;

        public NoGUI()
        {
            InitializeComponent();
            _BaoCaoNoBLL = new BLL.LapBaoCaoCongNoThangBLL();
        }

        public String getName()
        {
            return "LẬP BÁO CÁO CÔNG NỢ";
        }

        private void btn_TimKhachHang_Click(object sender, EventArgs e)
        {
            Form frm = GUI.qlnsGUI.isOpenForm(typeof(GUI.TraCuu_Update.KhachHangFrm));
            if (frm != null)
            {
                (frm as GUI.TraCuu_Update.KhachHangFrm).SetCallback(SetKhachHangFromTimKhachHang);
                frm.Focus();
            }
            else
            {
                frm = new GUI.TraCuu_Update.KhachHangFrm(SetKhachHangFromTimKhachHang);
                frm.Show(this);
            }
        }

        private void SetKhachHangFromTimKhachHang(DTO.KhachHangDTO dto)
        {
            DataGridViewRow curRow = gv_List.CurrentRow;
            if (curRow == null)
                curRow = gv_List.Rows[gv_List.Rows.Count - 1];
            curRow.Cells["MaKhachHang"].Value = dto.ID.ToString();
            curRow.Cells["HoTen"].Value = dto.HoTen;

            //gv_List.Rows[gv_List.Rows.Count - 1].Cells["MaKhachHang"].Value
            //    = dto.ID.ToString();
            //gv_List.Rows[gv_List.Rows.Count - 1].Cells["HoTen"].Value = dto.HoTen;
        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            List<BLL.LapBaoCaoCongNoThangBLL.KhachHangOnGui> list =
                new List<BLL.LapBaoCaoCongNoThangBLL.KhachHangOnGui>();
            foreach (DataGridViewRow r in gv_List.Rows)
            {
                //if (r.Cells["MaKhachHang"].Value == null ||
                //    r.Cells["MaKhachHang"].Value.ToString() == (-1).ToString())
                //{
                //    MessageBox.Show("Chọn Khách Hàng Trước");
                //    return;
                //}

                if (r.Cells["NoDau"].Value == null &&
                    r.Cells["PhatSinh"].Value == null)
                    continue;
                if (r.Cells["NoDau"].Value == null ||
                    r.Cells["PhatSinh"].Value == null)
                {
                    if (MessageBox.Show(
                        "Giá Trị Không Hợp Lệ, Bỏ Qua Khách Hàng Thứ " +
                        r.Cells["STT"].Value.ToString(),
                        "", MessageBoxButtons.YesNo)
                       == DialogResult.Yes)
                    {
                        continue;
                    }
                    else
                        return;
                }
                BLL.LapBaoCaoCongNoThangBLL.KhachHangOnGui kh =
                    new BLL.LapBaoCaoCongNoThangBLL.KhachHangOnGui();
                kh.ID = int.Parse(r.Cells["MaKhachHang"].Value.ToString());
                kh.NoDau = float.Parse(r.Cells["NoDau"].Value.ToString());
                kh.PhatSinh = float.Parse(r.Cells["PhatSinh"].Value.ToString());
                list.Add(kh);
            }

            if (list.Count < 1)
            {
                MessageBox.Show("Chọn Ít Nhất 1 Khách Hàng");
                return;
            }

            int rv = _BaoCaoNoBLL.LapBaoCao(cbx_Thang.SelectedIndex + 1, list);
            if (rv != 0)
            {
                MessageBox.Show("Không Thể Lập Báo Cáo Này, Kiểm Tra Lại Input");
            }
            else
            {
                gv_List.Rows.Clear();
                gv_List.Rows[gv_List.Rows.Count - 1].Cells["STT"].Value =
                gv_List.Rows.Count.ToString();
            }
        }

        private void btn_LamLai_Click(object sender, EventArgs e)
        {
            gv_List.Rows.Clear();
            gv_List.Rows[gv_List.Rows.Count - 1].Cells["STT"].Value =
               gv_List.Rows.Count.ToString();
        }

        private void InitThang()
        {
            String[] range = new String[12];
            for (int i = 0; i < 12; i++)
                range[i] = (i + 1).ToString();
            cbx_Thang.Items.AddRange(range);
            cbx_Thang.SelectedIndex = 0;
        }

        private void NoGUI_Load(object sender, EventArgs e)
        {
            InitThang();
            gv_List.Rows[gv_List.Rows.Count - 1].Cells["STT"].Value =
                gv_List.Rows.Count.ToString();

            Form frm = GUI.qlnsGUI.isOpenForm(typeof(GUI.TraCuu_Update.KhachHangFrm));
            if (frm != null)
                (frm as GUI.TraCuu_Update.KhachHangFrm).SetCallback(SetKhachHangFromTimKhachHang);
        }

        private void gv_List_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            gv_List.Rows[gv_List.Rows.Count - 1].Cells["STT"].Value =
                gv_List.Rows.Count.ToString();
        }

        private void gv_List_EditingControlShowing(object sender, 
            DataGridViewEditingControlShowingEventArgs e)
        {
            int clidx = (sender as DataGridView).CurrentCell.ColumnIndex;
            if (clidx == 3 ||
                clidx == 4)
            {
                e.Control.KeyPress -= new KeyPressEventHandler(ValidKey_KeyPress);
                e.Control.KeyPress += new KeyPressEventHandler(ValidKey_KeyPress);

                (e.Control as TextBox).TextChanged -= new System.EventHandler(SetNoCuoi_TextChanged);
                (e.Control as TextBox).TextChanged += new System.EventHandler(SetNoCuoi_TextChanged);
            }
        }

        private void ValidKey_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) &&
                !char.IsDigit(e.KeyChar) &&
                e.KeyChar != '.')
                e.Handled = true;
            if (e.KeyChar == '.' &&
                (sender as TextBox).Text.IndexOf('.') > -1)
                e.Handled = true;
        }

        private void SetNoCuoi_TextChanged(object sender, EventArgs e)
        {
            if ((sender as DataGridViewTextBoxEditingControl).Text == String.Empty)
                return;
            float vl = float.Parse(
                (sender as DataGridViewTextBoxEditingControl).Text.ToString());
            int ridx = (sender as DataGridViewTextBoxEditingControl).
                EditingControlDataGridView.CurrentCell.RowIndex;
            int clidx = (sender as DataGridViewTextBoxEditingControl).
                EditingControlDataGridView.CurrentCell.ColumnIndex;
            if (clidx == 3)
            {
                if ((sender as DataGridViewTextBoxEditingControl).
                    EditingControlDataGridView.Rows[ridx].Cells[4].Value == null)
                    (sender as DataGridViewTextBoxEditingControl).
                        EditingControlDataGridView.Rows[ridx].Cells[5].Value = vl.ToString();
                else
                {
                    (sender as DataGridViewTextBoxEditingControl).
                        EditingControlDataGridView.Rows[ridx].Cells[5].Value =
                        (float.Parse((sender as DataGridViewTextBoxEditingControl).
                    EditingControlDataGridView.Rows[ridx].Cells[4].Value.ToString()) +
                    vl).ToString();
                }
            }
            else if (clidx == 4)
            {
                if ((sender as DataGridViewTextBoxEditingControl).
                    EditingControlDataGridView.Rows[ridx].Cells[3].Value == null)
                    (sender as DataGridViewTextBoxEditingControl).
                        EditingControlDataGridView.Rows[ridx].Cells[5].Value = vl.ToString();
                else
                {
                    (sender as DataGridViewTextBoxEditingControl).
                        EditingControlDataGridView.Rows[ridx].Cells[5].Value =
                        (float.Parse((sender as DataGridViewTextBoxEditingControl).
                    EditingControlDataGridView.Rows[ridx].Cells[3].Value.ToString()) +
                    vl).ToString();
                }
            }
        }
    }
}
